package io.github.javpower.imagerex.controller;

import io.github.javpower.imagerex.model.Image;
import io.github.javpower.imagerex.req.ImageReq;
import io.github.javpower.imagerex.res.PageResult;
import io.github.javpower.imagerex.service.VectorExService;
import io.github.javpower.vectorexbootstater.core.VectoRexResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/image/upload")
@Tag(name = "图片管理", description = "用于管理图片及其特征向量的接口")
public class UploadController {

    private final VectorExService vectorExService;

    @Autowired
    public UploadController(VectorExService vectorExService) {
        this.vectorExService = vectorExService;
    }

    @PostMapping("/delete")
    @Operation(summary = "删除图片", description = "根据图片名称删除图片及其特征向量")
    public String deleteImageByName(@RequestBody ImageReq req) {
        return vectorExService.deleteImageByName(req.getName());
    }

    @PostMapping("/single")
    @Operation(summary = "上传单张图片", description = "上传单张图片并提取特征向量")
    public String uploadSingleImage(@RequestParam("file") MultipartFile file) {
        return vectorExService.uploadSingleImage(file);
    }

    @PostMapping("/folder")
    @Operation(summary = "上传文件夹", description = "上传文件夹中的多个文件并提取特征向量")
    public String uploadFolderImage(@RequestParam("files[]") List<MultipartFile> files) {
        // 调用服务处理文件上传逻辑
        return vectorExService.uploadFolderImage(files);
    }


    @PostMapping("/zip")
    @Operation(summary = "上传zip文件", description = "上传zip文件，自动解压并处理其中的图片")
    public String uploadZipFile(@RequestParam("file") MultipartFile file) {
        return vectorExService.uploadZipFile(file);
    }

    @PostMapping("/deleteBatch")
    public String deleteBatch(@RequestBody Map<String, List<String>> body) {
        List<String> names = body.get("names");
        for (String name : names) {
            vectorExService.deleteImageByName(name);
        }
        return "已删除 " + names.size() + " 张图片";
    }


}